package com.ma5d.driver.customer.service.impl;

import cn.binarywang.wx.miniapp.bean.Watermark;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ma5d.driver.customer.mapper.CustomerInfoMapper;
import com.ma5d.driver.customer.mapper.CustomerLoginLogMapper;
import com.ma5d.driver.model.form.UpdateWxPhoneForm;
import com.ma5d.driver.model.vo.customer.CustomerLoginVo;
import com.ma5d.driver.model.entity.customer.CustomerInfo;
import com.ma5d.driver.model.entity.customer.CustomerLoginLog;
import com.ma5d.driver.customer.service.CustomerInfoService;
import jakarta.annotation.Resource;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class CustomerInfoServiceImpl implements CustomerInfoService {

    @Resource
    private WxMaService wxService;
    @Resource
    private CustomerInfoMapper customerInfoMapper;
    @Resource
    private CustomerLoginLogMapper customerLoginLogMapper;

    @Override
    public Long login(String code) throws WxErrorException {
        // 1.获取code值， 使用微信工具包对象，获取微信用户唯一标识openid
        WxMaJscode2SessionResult sessionInfo = wxService.getUserService().getSessionInfo(code);
        String openid = sessionInfo.getOpenid();

        // 2. 根据openid查询数据表， 判断是否是第一次登录
        LambdaQueryWrapper<CustomerInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomerInfo::getWxOpenId, openid);
        CustomerInfo customerInfo = customerInfoMapper.selectOne(lambdaQueryWrapper);

        // 3. 如果第一次登录，添加信息到用户表
        if (customerInfo == null) {
            customerInfo = new CustomerInfo();
            customerInfo.setNickname(String.valueOf(System.currentTimeMillis()));
            customerInfo.setAvatarUrl("https://wx.qlogo.cn/mmhead/jTy9FNWibwuMGicicWdcFLSJgXVaurpxicDWycGV8Mznia9LT6BF1iam4BSx9okzftv8t08Ozgr4frsLQ/0");
            customerInfo.setWxOpenId(openid);
            customerInfoMapper.insert(customerInfo);
        }

        // 4. 记录日志 TODO 异步实现
        CustomerLoginLog customerLoginLog = new CustomerLoginLog();
        customerLoginLog.setCustomerId(customerInfo.getId());
        customerLoginLog.setMsg("小程序登录");
        customerLoginLogMapper.insert(customerLoginLog);

        // 5. 返回用户id
        return customerInfo.getId();
    }

    @Override
    public CustomerLoginVo getCustomerInfo(Long customerId) {
        // 业务逻辑上来说，走到这里的已经在数据库并且cache打开会话了,所以绝不会返回null
        CustomerInfo customerInfo = customerInfoMapper.selectById(customerId);
        CustomerLoginVo customerLoginVo = new CustomerLoginVo();
        BeanUtils.copyProperties(customerInfo, customerLoginVo);
        String phone = customerInfo.getPhone();
        customerLoginVo.setIsBindPhone(StringUtils.hasText(phone));
        return customerLoginVo;
    }

    @Override
    public Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
        // 1.根据 code 值获取微信绑定手机号
        // 没有企业级微信公众号的手机，临时处理
        // WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(updateWxPhoneForm.getCode());
        WxMaPhoneNumberInfo phoneNoInfo = new WxMaPhoneNumberInfo();
        phoneNoInfo.setPhoneNumber("19966593117");
        phoneNoInfo.setPurePhoneNumber("19966593117");
        phoneNoInfo.setCountryCode("+86");
        phoneNoInfo.setWatermark(new Watermark());
        String phoneNumber = phoneNoInfo.getPhoneNumber();

        // 更新用户信息
        Long customerId = updateWxPhoneForm.getCustomerId();
        CustomerInfo customerInfo = customerInfoMapper.selectById(customerId);
        customerInfo.setPhone(phoneNumber);
        customerInfoMapper.updateById(customerInfo);
        return true;
    }
}
